#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：8.使用异步上下文管理器控制数据库连接.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-12-17 15:04
'''
import asyncio

import aiomysql

async def main():
    # aiomysql_db = await aiomysql.connect(host='localhost', port=3306, user='root', password='123456', db='py_spider')
    # cursor = await aiomysql_db.cursor()

    async with aiomysql.connect(host='localhost', port=3306, user='root', password='123456', db='py_spider') as aiomysql_db:
        async with aiomysql_db.cursor() as cursor:
            await cursor.execute('select * from tx_work;')
            results = await cursor.fetchall()
            print(results)

            print("-----" * 20)

            print(cursor.description)  # 可以使用当前方法查询表中的字段信息


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())